Completed
Pull Request — master (#1653)
by Aristeides
04:15 queued 02:06
created

kirkiDependencies.evaluate   C

Complexity

Conditions 26
Paths 24

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 26
nc 24
nop 3
dl 0
loc 53
rs 6.1024
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A 0 8 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like kirkiDependencies.evaluate often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
var kirkiDependencies = {
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
2
3
	listenTo: {},
4
5
	init: function() {
6
		var self = this;
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
7
8
		wp.customize.control.each( function( control ) {
9
			self.showKirkiControl( control );
10
		} );
11
12
		_.each( self.listenTo, function( slaves, master ) {
13
			_.each( slaves, function( slave ) {
14
				wp.customize( master, function( setting ) {
15
				    var setupControl = function( control ) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
16
				        var setActiveState,
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
17
						    isDisplayed;
18
19
						isDisplayed = function() {
20
							return self.showKirkiControl( wp.customize.control( slave ) );
21
						};
22
						setActiveState = function() {
23
							control.active.set( isDisplayed() );
24
						};
25
26
						setActiveState();
27
						setting.bind( setActiveState );
28
						control.active.validate = isDisplayed;
29
				    };
30
				    wp.customize.control( slave, setupControl );
31
				} );
32
			});
33
		});
34
	},
35
36
	/**
37
	 * Should we show the control?
38
	 *
39
	 * @since 3.0.17
40
	 * @param {string|object} control - The control-id or the control object.
41
	 * @returns {bool}
42
	 */
43
	showKirkiControl: function( control ) {
44
		var self = this,
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
45
		    show = true;
46
47
		if ( _.isString( control ) ) {
48
			control = wp.customize.control( control );
0 ignored issues
show
Comprehensibility Best Practice introduced by
This re-assigns to the parameter control. Re-assigning to parameters often makes code less readable, consider introducing a new variable instead.
Loading history...
49
		}
50
51
		// Exit early if control not found or if "required" argument is not defined.
52
		if ( 'undefined' === typeof control || ( control.params && _.isEmpty( control.params.required ) ) ) {
53
			return true;
54
		}
55
56
		// Loop control requirements.
57
		_.each( control.params.required, function( requirement ) {
58
			if ( 'undefined' === typeof wp.customize.control( requirement.setting ) ) {
59
				show = true;
60
				return;
61
			}
62
			var requirementShow = self.evaluate( requirement.value, wp.customize.control( requirement.setting ).setting._value, requirement.operator );
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
63
64
			self.listenTo[ requirement.setting ] = self.listenTo[ requirement.setting ] || [];
65
			if ( -1 === self.listenTo[ requirement.setting ].indexOf( control.id ) ) {
66
				self.listenTo[ requirement.setting ].push( control.id );
67
			}
68
69
			if ( ! requirementShow ) {
70
				show = false;
71
			}
72
		} );
73
		return show;
74
	},
75
76
	/**
77
	 * Figure out if the 2 values have the relation we want.
78
	 *
79
	 * @since 3.0.17
80
	 * @param {mixed} value1 - The 1st value.
81
	 * @param {mixed} value2 - The 2nd value.
82
	 * @param {string} operator - The comparison to use.
83
	 * @returns {bool}
84
	 */
85
	evaluate: function( value1, value2, operator ) {
86
		var found = false,
0 ignored issues
show
Coding Style introduced by
As per coding-style, prefer block-scoped variables using let or const which have better semantics than var.

Since ECMAScript 6, you can create block-scoped vars or constants with the keywords let or const. These variables/constants are only valid in the code block where they have been declared.

Consider the following two pieces of code:

if (true)
 {
    var x = "Hello, Stonehenge!";
}

console.log(x); //prints Hello, Stonehenge! to the console

and

if (true)
 {
    let x = "Hello, Stonehenge!";
}

console.log(x); //ReferenceError: x is not defined

The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.

To know more about this ECMA6 feature, look at the MDN pages on let and const.

Loading history...
Unused Code introduced by
The assignment to variable found seems to be never used. Consider removing it.
Loading history...
87
		    result = null;
88
89
		if ( '===' === operator ) {
90
			result = value1 === value2;
91
		} else if ( '==' === operator || '=' === operator || 'equals' === operator || 'equal' === operator ) {
92
			result = value1 == value2; // jshint ignore:line
93
		} else if ( '!==' === operator ) {
94
			result = value1 !== value2;
95
		} else if ( '!=' === operator || 'not equal' === operator ) {
96
			result = value1 != value2; // jshint ignore:line
97
		} else if ( '>=' === operator || 'greater or equal' === operator || 'equal or greater' === operator ) {
98
			result = value2 >= value1;
99
		} else if ( '<=' === operator || 'smaller or equal' === operator || 'equal or smaller' === operator ) {
100
			result = value2 <= value1;
101
		} else if ( '>' === operator || 'greater' === operator ) {
102
			result = value2 > value1;
103
		} else if ( '<' === operator || 'smaller' === operator ) {
104
			result = value2 < value1;
105
		} else if ( 'contains' === operator || 'in' === operator ) {
106
			if ( _.isArray( value2 ) ) {
107
				found = false;
108
				_.each( value2, function( index, value ) {
109
					if ( _.isNumber( value ) ) {
110
						value = parseInt( value, 10 );
0 ignored issues
show
Comprehensibility Best Practice introduced by
This re-assigns to the parameter value. Re-assigning to parameters often makes code less readable, consider introducing a new variable instead.
Loading history...
111
					}
112
					if ( value1.indexOf( value ) > -1 ) {
113
						found = true;
114
					}
115
				} );
116
				return found;
117
			} else if ( _.isObject( value2 ) ) {
118
				found = false;
119
				if ( ! _.isUndefined( value2[ value1 ] ) ) {
120
					found = true;
121
				}
122
123
				_.each( value2, function( subValue ) {
124
					if ( value1 === subValue ) {
125
						found = true;
126
					}
127
				} );
128
				return found;
129
			} else if ( _.isString( value2 ) ) {
130
				return value1.indexOf( value2 ) > -1;
131
			}
132
		}
133
		if ( null === result ) {
134
			return true;
135
		}
136
		return result;
137
	}
138
};
139
140
jQuery( document ).ready( function() {
141
	kirkiDependencies.init();
142
} );
143